<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/14
 * Time: 16:57
 */
namespace app\admin\controller;
use think\Db;
class House  extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $params = input('param.');
            $map = [];
            if(isset($h_fix)&&$h_fix != ""){
                $map['h_fix'] = $h_fix;
            }
            if(isset($h_type)&&$h_type != ""){
                $map['h_type'] = $h_type;
            }
            if(isset($h_shui)&&$h_shui != ""){
                $map['h_shui'] = $h_shui;
            }
            if(isset($key)&&$key!="")
            {
                $map['h_title'] = ['like',"%" . $key . "%"];
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $map['h_sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $map['h_shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $map['h_qu'] = $params['qu'];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('house')->where($map)->count();
            $lists = Db::name('house')->alias('u')
                ->join('up_area a1', 'u.h_sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.h_shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.h_qu = a3.district_id', 'left')
                ->where($map)
                ->page($page,$limit)
                ->order('h_id desc')
                ->field('u.*,a1.district sheng,a2.district shi,a3.district qu')
                ->select ();
            foreach ($lists as $key=>&$vo){
                if($vo['h_shis'] > 5){$vo['h_shis']='5室以上';}else{$vo['h_shis'] = $vo['h_shis'] . '室';};
                if($vo['h_tings'] > 4){$vo['h_tings']='4厅以上';}else{$vo['h_tings'] = $vo['h_tings'] . '厅';};
                if($vo['h_weis'] > 4){$vo['h_weis']='4卫以上';}else{$vo['h_weis'] = $vo['h_weis'] . '卫';};
                $vo['h_hx'] = $vo['h_shis'] . $vo['h_tings'] . $vo['h_weis'];
                if($vo['h_images'] != ''){
                    $interlocution_img = trim($vo['h_images'],',');
                    $photo = explode (',',$interlocution_img);
//                    $lists[$key]['picture'] = $photo[0];
//                    unset($photo[0]);
                    $lists[$key]['photo'] = array_values($photo);
                }
                $lists[$key]['disname'] = '无';
                if($vo['h_dis_id'] > 0){
                    $lists[$key]['disname'] = Db::name('district')->where('dis_id',$vo['h_dis_id'])->value('dis_name');
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $res = Db::name('house')->where('h_id',input('id'))->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    
    public function update(){
        if(request()->isPost ()){
            extract(input());
            $h_type = Db::table('up_house')->where('h_id', $id)->value('h_type');
            if($h_type != 1){
                $data['h_price'] = $h_price;
                $data['h_first'] = $h_first;
                $data['h_shui'] = $h_shui;
            }else{
                $data['h_kan'] = $h_kan;
            }
            
            $data['h_title'] = $h_title;
            $data['h_contact'] = $h_contact;
            $data['h_mianji'] = $h_mianji;
            $data['h_fix'] = $h_fix;
            $data['h_shis'] = $h_shis;
            $data['h_tings'] = $h_tings;
            $data['h_weis'] = $h_weis;
            $data['h_floor'] = $h_floor;
            $data['h_jianjie'] = $h_jianjie;
            $data['h_sign'] = ','.implode(',',$h_sign).',';
    
            $res = Db::table('up_house')->where('h_id',$id)->update($data);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $info = Db::table('up_house')->alias('h')
            ->join('up_district d','h.h_dis_id=d.dis_id','LEFT')
            ->join('up_area a1', 'h.h_sheng = a1.district_id', 'left')
            ->join('up_area a2', 'h.h_shi = a2.district_id', 'left')
            ->join('up_area a3', 'h.h_qu = a3.district_id', 'left')
            ->where(['h_id'=>input('id')])
            ->field('h.*,d.dis_name,a1.district sheng,a2.district shi,a3.district qu')
            ->find();
        $info['h_signs'] = explode(',',trim($info['h_sign'],','));
        $diss = Db::name('district')->field('dis_id,dis_name')->select();
        return $this->fetch('house/update',['info'=>$info,'diss'=>$diss]);
    }
}